﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using HZ;

namespace WebDemo
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            new HZ.DrugTrade.BLL.BLL_Cache();
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if ((HttpContext.Current.Request.Form.Count > 0) && ((HttpContext.Current.Request.UrlReferrer == null) || (string.Compare(HttpContext.Current.Request.UrlReferrer.Host, HttpContext.Current.Request.Url.Host, true) != 0)))
            {
                HttpContext.Current.Response.Write("系统检测到非法提交");
                //log4netManager.LogWarn("HttpMpdule", string.Format("非法提交：IP地址：{0}，URL：{1}", Fun.GetClientIP(), HttpContext.Current.Request.Url.ToString()), null);
                HttpContext.Current.Response.End();
            }
            foreach (string str in HttpContext.Current.Request.QueryString)
            {
                if (str != HZ.DrugTrade.Web.Constants.QUERYSTRING_RETURN_URL) //returnUrl不做检查，防止误报
                {
                    if (Fun2.MaySqlInject(HttpContext.Current.Request.QueryString[str].ToString()))
                    {
                        this.Inject_Handle(HttpContext.Current.Request.QueryString[str].ToString(), "QueryString");
                    }
                }
            }
            foreach (string str2 in HttpContext.Current.Request.Form)
            {
                if ((!(str2 == "__VIEWSTATE") && !(str2 == "__EVENTVALIDATION")) && Fun2.MaySqlInject(HttpContext.Current.Request.Form[str2].ToString()))
                {
                    this.Inject_Handle(HttpContext.Current.Request.Form[str2].ToString(), "Form");
                }
            }
            foreach (string str3 in HttpContext.Current.Request.Cookies)
            {
                if (Fun2.MaySqlInject(HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[str3].Value)))
                {
                    this.Inject_Handle(HttpContext.Current.Request.Cookies[str3].ToString(), "Cookies");
                }
            }

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }
       
        protected void Application_Error(object sender, EventArgs e)
        {
            // Exception ex = Server.GetLastError().InnerException;
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

        private void Inject_Handle(string str, string src)
        {
            HttpContext.Current.Response.Write("<center><span style=\"font-size: 13px; font-family: Arial;\">Http安全模块信息检测：系统检测到非法字符串输入。<br />");
            HttpContext.Current.Response.Write(string.Format("非法字符串信息：{0}<br />", str));
            HttpContext.Current.Response.Write("<a href=\"\">返回主页</a></span></center>");
            //log4netManager.LogWarn("HttpMpdule", string.Format("SQL注入攻击检测：注入IP:{0}，注入内容： {1}，来源：{2}", Fun.GetClientIP(), str, src), null);
            HttpContext.Current.Response.End();
        }

 

    }
}